package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Process;
import org.example.erp.domain.ProcessDetails;
import org.example.erp.service.ProcessDetailsService;
import org.example.erp.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 工序表设置
@RestController
@RequestMapping("/api")
public class ProcessController {
    @Autowired
    ProcessService processService;
    @Autowired
    ProcessDetailsService processDetailsService;

    @GetMapping("/processSettings/find")
    public DataResult find(
            @RequestParam(value = "processName", required = false) String processName,
            @RequestParam(value = "processCode", required = false) String processCode,
            @RequestParam(value = "isEnabled", required = false) String isEnabled,
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        // 检查所有查询参数是否为空
        boolean allParamsEmpty = (processName == null || processName.isEmpty()) &&
                (processCode == null || processCode.isEmpty()) &&
                (isEnabled == null || isEnabled.isEmpty() && (currentPage == -1 && pageSize == -1));

        QueryWrapper<Process> queryWrapper = new QueryWrapper<>();
        if (processName != null && !processName.isEmpty()) {
            queryWrapper.lambda().like(Process::getProcessName, processName);
        }
        if (processCode != null && !processCode.isEmpty()) {
            queryWrapper.lambda().like(Process::getProcessCode, processCode);
        }
        if (isEnabled != null && !isEnabled.isEmpty()) {
            queryWrapper.lambda().like(Process::getIsEnabled, isEnabled);
        }
        queryWrapper.orderByDesc("process_code");
        if (allParamsEmpty) {
            queryWrapper.lambda().eq(Process::getIsEnabled,0);//查询启用的
            // 不使用分页查询
            List<Process> processes = processService.list(queryWrapper);
            return DataResult.success(processes);
        } else {
            // 使用分页查询
            Page<Process> page = new Page<>(currentPage, pageSize);
            page = processService.page(page, queryWrapper);
            return DataResult.success(page);
        }
    }

    @GetMapping("/processSettings/findById")
    public DataResult findById(@RequestParam String id) {
        Process process = processService.getById(id);
        return DataResult.success(process);
    }

    @PutMapping("/processSettings/update")
    public DataResult update(@RequestBody Process process) {
        boolean b = processService.updateById(process);
        return DataResult.success(b ? "成功" : "失败");
    }

    @DeleteMapping("/processSettings/delete")
    public DataResult delete(@RequestBody List<String> ids) {
        boolean b = processService.deleteDetails(ids);
        return DataResult.success(b ? "成功" : "失败");
    }

    @PostMapping("/processSettings/insert")
    public DataResult add(@RequestBody Process process) {
        boolean save = processService.save(process);
        return DataResult.success(save ? "成功" : "失败");
    }

    // 查询工序详情
    @GetMapping("/processSettings/findDetails")
    public DataResult findDetails(@RequestParam String processCode) {
        Process list = processService.findDetails(processCode);
        if (list == null || list.getProcessDetails() == null) {
            return DataResult.success(list);
        }
        boolean allNull = true;
        for (ProcessDetails p : list.getProcessDetails()) {
            if (p.getId() != null) {
                allNull = false;
                break;
            }
        }
        if (allNull) {
            list.setProcessDetails(null);
        }
        return DataResult.success(list);
    }

    // 添加工序步骤
    @PostMapping("/processSettings/addSteps")
    public DataResult addSteps(@RequestBody ProcessDetails processDetails) {
        boolean save = processDetailsService.save(processDetails);
        return DataResult.success(save ? "成功" : "失败");
    }

    // 修改工序步骤
    @PutMapping("/processSettings/updateSteps")
    public DataResult updateSteps(@RequestBody ProcessDetails processDetails) {
        boolean b = processDetailsService.updateById(processDetails);
        return DataResult.success(b ? "成功" : "失败");
    }

    // 删除工序步骤
    @DeleteMapping("/processSettings/deleteStep")
    public DataResult deleteSteps(@RequestBody List<String> id) {
        boolean b = processDetailsService.removeByIds(id);
        return DataResult.success(b ? "成功" : "失败");
    }
}
